/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.interpreter.frontends.owlxml;

import java.util.List;

/**
 *
 * @author Laurent WOUTERS
 */
public class AnnotationLoader {
    public static final String nodeIRI = "IRI";
    public static final String nodeAbbreviatedIRI = "AbbreviatedIRI";

    public static void loadAnnotations(xowl.lang.owl2.Annotation_OR_Axiom_OR_Ontology annotated, OntologyLoader ontologyLoader, List<org.w3c.dom.Node> children, xowl.interpreter.frontends.LexicalContext context) {
        for (int i=0; i!=children.size(); i++) {
            org.w3c.dom.Node child = children.get(i);
            if (!xowl.interpreter.frontends.VocOWL2.ontoAnnotation.equals(child.getNodeName()))
                return;
            annotated.addAnnotations(load(ontologyLoader, child, context));
            children.remove(0);
            i--;
        }
    }

    public static xowl.lang.owl2.Annotation load(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        xowl.lang.owl2.Annotation annotation = new xowl.lang.owl2.Annotation();
        List<org.w3c.dom.Node> children = ExpressionLoader.getElements(node);
        loadAnnotations(annotation, ontologyLoader, children, context);
        annotation.setAnnotProperty(loadAnnotationProperty(ontologyLoader, children.get(0), context));
        annotation.setAnnotValue(loadAnnotationValue(ontologyLoader, children.get(1), context));
        return annotation;
    }

    public static xowl.lang.owl2.IRI loadAnnotationProperty(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        return ontologyLoader.getFullIRI(node.getTextContent());
    }
    public static xowl.lang.owl2.IRI loadAnnotationIRI(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        return ontologyLoader.getFullIRI(node.getTextContent());
    }
    public static xowl.lang.owl2.AnnotationSubject loadAnnotationSubject(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        if (node.getNodeName().equals(xowl.interpreter.frontends.VocOWL2.entityAnonymousIndividual))
            return ExpressionLoader.load_AnonymousIndividual(ontologyLoader, node, context);
        return loadAnnotationIRI(ontologyLoader, node, context);
    }
    public static xowl.lang.owl2.AnnotationValue loadAnnotationValue(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        if (node.getNodeName().equals(xowl.interpreter.frontends.VocOWL2.entityAnonymousIndividual))
            return ExpressionLoader.load_AnonymousIndividual(ontologyLoader, node, context);
        if (node.getNodeName().equals(xowl.interpreter.frontends.VocOWL2.expLiteral))
            return ExpressionLoader.load_Literal(ontologyLoader, node, context);
        return loadAnnotationIRI(ontologyLoader, node, context);
    }
}
